<?php
namespace Api\Controller\V3;
use Think\Controller;
/**
* @name Lukeyan
* @abstract 申明变量/类/方法
* @access 指明这个变量、类、函数/方法的存取权限
* @author 函数作者的名字和邮箱地址
* @category  组织packages
* @copyright 指明版权信息
* @const 指明常量
* @deprecate 指明不推荐或者是废弃的信息MyEclipse编码设置
* @example 示例
* @exclude 指明当前的注释将不进行分析，不出现在文挡中
* @final 指明这是一个最终的类、方法、属性，禁止派生、修改。
* @global 指明在此函数中引用的全局变量
* @include 指明包含的文件的信息
* @link 定义在线连接
* @module 定义归属的模块信息
* @modulegroup 定义归属的模块组
* @package 定义归属的包的信息
* @param 定义函数或者方法的参数信息
* @return 定义函数或者方法的返回信息
* @see 定义需要参考的函数、变量，并加入相应的超级连接。
* @since 指明该api函数或者方法是从哪个版本开始引入的
* @static 指明变量、类、函数是静态的。
* @throws 指明此函数可能抛出的错误异常,极其发生的情况
* @todo 指明应该改进或没有实现的地方
* @var 定义说明变量/属性。
* @version 定义版本信息
*/

/*
 * APP 获取到的杂七杂八的功能
 */
class IndexController extends CommonController{
    public function index(){
        header("Content-type:text/html;charset=utf8;");
        echo "众测天下";
    }
    
    
    /*
     * 首页广告列表
     */
    public function ad_lists() {
        $mid = trim(I('get.mid'));
        $password = trim(I('get.password'));
        
        $DB = M('adv');
        $map['state']=1;
        $map['ttype']=2;
        
        
        
        /*
         * 如果是项目信息，获取fdid状态
         * 
         */
        
        //判断用户是否是苏州参加活动的  waiting
        
        $DB_suzhou = M("acsuzhou_member_info");
        $map_suzhou['mid']=$mid;
        $res_suzhou = $DB_suzhou->where($map_suzhou)->find();
        
        // if(!empty($res_suzhou))
        // {
        //     if($res_suzhou['is_exam'])
        //     {
        //         $res = $DB->where($map)->order("rank DESC")->select();
        //     }
        //     else
        //     {
        //         $map['actype']=array('neq',1);
                
        //         $res = $DB->where($map)->order("rank DESC")->select();
        //     }
        // }
        // else
        // {
        //     $map['actype']=array('neq',1);
                
        //     $res = $DB->where($map)->order("rank DESC")->select();
        // }

        // $map['actype']=array('neq',1);
                
        $res = $DB->where($map)->order("rank DESC")->select();
        
        
        
        
        foreach ($res as $key => $value) {
            
            //未登录用户，剔除掉 活动入口
            
            if($value['type']==0)
            {
                $res[$key]['fd_state']=$this->_get_state_by_fdid($value['fdid']);
            }
            else
            {
                $res[$key]['fd_state']=0;
            }
            unset($res[$key]['content']);

            /*
            *  增加入口的判断
            */
            if(!empty($res_suzhou))
            {
                $res[$key]['ifenter_activity'] = 1;
            }
            else
            {
                $res[$key]['ifenter_activity'] = 0;
            }
            
            
        }
        $this->json_output($res);
    }
    
    
    /*
     * 广告详情（单单是数据展示的一种）
     * 没有包括 H5 页面、跳转到指定项目这些
     */
    public function ad_detail() {
        $adid = I('get.adid');  //获取广告id
        
        $DB = M('adv');
        $res = $DB->find($adid);
        
        //获取文章的评论资料
        
        
        $this->json_output($res);
        
    }
    
    /*
     * 获取首页的热点资讯内容
     */
    public function latest_news() {
        $pagecount = I('get.pagecount');
        $rank_type = I("get.rank_type");
        
        if($rank_type==1)
        {
            //按热度排序
            if(empty($pagecount))
            {
                $pagecount=0;
            }

            $startconnt = $pagecount*10;
            $News_db = M('news');
            $News_map['active'] = 1;
            $News_map['news_type'] = 0;
            $res = $News_db->where($News_map)->limit($startconnt,10)->field('id,title,thumb,news_type,(comment_count+collection_count) as hot,author')->order("hot DESC,rank DESC,ctime DESC")->select();
            
            foreach ($res as $key => $value) {
                $res[$key]['comment_count']=  $this->get_one_comments_count($value['id'],2);
                // $res[$key]['title'] = html_entity_decode($value['title']);
            }

            if(!empty($res))
            {
                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

                foreach ($res as $key => $value) {
                    $res[$key]['url']=$site_domain.U('M/News/details_for_app',array('newsid'=>$value['id']));
                }
                $this->json_output($res);
            }
            else
            {
                $result['err']=1;
                $result['msg']="暂无新数据";
                $result['content']="";
                echo json_encode($result);
            }
            
        }
        else
        {
           //增加排序的标准，rank_type 0 为 默认排序，按照时间来排序 
            if(empty($pagecount))
            {
                $pagecount=0;
            }

            $startconnt = $pagecount*10;
            $map['active']=1;
            $map['news_type'] = 0;

            $DB = M("news");

            $res = $DB->where($map)->field('id,title,thumb,author')->order("rank DESC")->limit($startconnt,10)->select();
            foreach ($res as $key => $value) {
                $res[$key]['comment_count']=  $this->get_one_comments_count($value['id'],2);
            }

            if(!empty($res))
            {
                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

                foreach ($res as $key => $value) {
                    $res[$key]['url']=$site_domain.U('M/News/details_for_app',array('newsid'=>$value['id']));
                }
                $this->json_output($res);
            }
            else
            {
                $result['err']=1;
                $result['msg']="暂无新数据";
                $result['content']="";
                echo json_encode($result);
            }
        }
        
        
        
        
    }
    
    /*
     * 获取指定的热点资讯
     */
    public function news_get_one() {
        $mid  = I('get.mid');
        $newsid = I('get.newsid');
        $ctype = I('get.ctype');
        $DB = M("news");
        
        $res = $DB->find($newsid);
        unset($res['content']);
        //判断用户是否关注了项目
        $res['ifcollection']=  $this->chk_if_member_follow_founding($mid, $newsid,$ctype);
        /*
         * 获取新闻的评论
         */
        
        
        //热门评论
        $res['comment_count']=  $this->get_one_comments_count($newsid,$ctype);
        $res['hot_comments']=  $this->get_one_hot_comments($newsid,$ctype);
        
        $this->json_output($res);
        
    }





    
    /*
     * 新闻资讯的评论
     */
    public function news_one_comment() {
        /*
         * 获取到需要评论的资料
         * 
         */
        $res = R("Api/V2/Widget/comment_add",array('pid'=>0,'mid'=>200,'content'=>"3333",'ctype'=>1,'for_id'=>10));
        if($res)
        {
            $result['err']=0;
        }
        else
        {
            $result['err']=1;
        }
        echo json_encode($result);
    }
    
    
    
    /*
     * 获取指定的众测的评论信息
     * 根据项目id 来获取评论
     * 
     */
    public function get_article_comments() {
        $id = I("get.id");
        $pid = I("get.pid");
        $pagecount = I("get.pagecount");
        $ctype = I('get.ctype');
        if(empty($pagecount))
        {
            $pagecount=0;
        }
        
        $startcount = $pagecount*10;
        
        
        $DB = M("comments");
        
        $map['for_id']=$id;
        if(empty($ctype)){

            $map['ctype'] = 2;
        }else{

            $map['ctype'] = $ctype;
        }

        if(!empty($pid)){

            $map['pid']= $pid;
        }else{

            $map['pid'] = 0;
        }

        // print_r($map); die;
        $res = $DB->where($map)->limit($startcount,10)->select();
        
        if(!empty($res))
        {
            foreach ($res as $key => $value) {
                /*
                * 获取单个用户的头像信息
                */
                $res[$key]['avatar']=  $this->_get_one_avatar($value['mid']);
                $res[$key]['username']=  $this->_get_member_nickname($value['mid']);
                $res[$key]['child_comments'] = $this->_get_child_comment($value['id']);
            }
            $this->json_output($res);
        }
        else
        {
            $result="";
            $this->json_output($result);
        }
        
    }
    
    /*
     * 针对文章进行评论
     */
    public function add_article_comment() {
        $mid = I('get.mid');
        $password = I('get.password');
        $ctype = I('get.ctype');

        $chk_user = $this->chk_user($mid, $password);
        
        if($chk_user)
        {
            $fdid = I("get.id");
            $pid = I("get.pid");
            $content = I("get.content");

            if(empty($pid)){

                $data['pid'] = 0;
            }else{

                $data['pid'] = $pid;
            }
            
            $data['mid']=$mid;
            $data['content']=$content;
            if(empty($ctype)){

                $data['ctype']= 2;
            }else{
                if($ctype==2)
                {
                    $data['ctype']= 2;
                }
                else
                {
                   $data['ctype']= 6; 
                }

                
            }
            $data['for_id']=$fdid;
            $data['ctime']=I("server.REQUEST_TIME");
            $data['ishidden']=0;
            
            $DB = M("comments");
            
            //对用户的评论进行检测查看是否含有敏感字
            $chk_back = chk_bad_words($content,1);
            if($chk_back){

                echo json_encode(2);
            }else{

                $id = $DB->add($data);
                if($id)
                {
                    if($pid>0)
                    {
                        send_msg_to_goal_user($id);
                        $DB_comment = M("comments");
                        $DB_comment->where("id=".$pid)->setInc("comment_count");

                    }
                    /*
                    * 增加用户的健康豆
                    */
                    $this->member_action_bonus('comment', $mid, 1);
                    $this->json_output($id);
                }
            }

        }
        else
        {
            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            echo json_encode($res);
        }
        
    }
    
    /*
     * 增加文章评论的点赞
     */
    public function add_article_comment_like() {
        $mid = I('get.mid');
        $password = I('get.password');
        
        $chk_user = $this->chk_user($mid, $password);
        
        if($chk_user)
        {
            $cid = I("get.cid");


            $data['cid']=$cid;
            $data['mid']=$mid;
            $data['ctime']=I("server.REQUEST_TIME");
            
            $DB = M("comment_likes");
            
            $id = $DB->add($data);
            
            if($id)
            {
                $DB_comment = M("comments");
                $DB_comment->where("id=".$cid)->setInc("likecount");
//                print_r($DB_comment->getLastSql());
                
                $this->json_output($id);
            }
        }
        else
        {
            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            echo json_encode($res);
        }
    }
    
    /*
     * 针对文章的收藏功能
     */
    
    public function add_article_collection() {
       /*
        * 收藏or 取消收藏
        * 0 取消收藏
        * 1 收藏
        * 
        * ctype 2 资讯，1 项目
        */
        $mid = I('get.mid');
        $password = I('get.password');
        $ctype = I('get.ctype');
        $chk_user = $this->chk_user($mid, $password);
        
        if($chk_user)
        {
            $forid = I("get.id");
            
            $ifcollection = I("get.ifcollection");
            
            $DB = M("collections");
       
            if($ifcollection==0)
            {
                if(empty($ctype)){

                    $ctype = 2;
                }else{

                    $map['ctype'] = $ctype;
                }
                $map['for_id']=$forid;
                $map['mid']=$mid;

                $DB->where($map)->delete();
                
                $this->json_output($forid);
            }
            else
            {
                //新增
                $data['ctype']=$ctype;
                $data['for_id']=$forid;
                $data['mid']=$mid;
                $data['ctime']=I("server.REQUEST_TIME");


                $id =$DB->add($data,array(),true);
                if($id)
                {
                    $this->json_output($id);
                }

            }
           
        }
        else
        {
            $res['err']=1;
            $res['msg']="用户登录账户或密码有误".ACTION_NAME;
            echo json_encode($res);
        }
        
        
       
   }
    
    
    /*
     * 获取指定热点资讯的分享资料
     */
    public function get_share_of_news() {
        $newsid = I('get.newsid');
        
        $type=I("get.type");
        
        if(empty($type))
        {
            $type=1;
        }
        switch ($type) {
            case 1:
                $DB = M("news");

                $res = $DB->find($newsid);

                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

                $share['title']=$res['title'];
        //        $share['desc']=$res['title'];
                $share['desc']="食品安全靠大家，一步一步更安全，我觉得这个信息很实用……";
                $share['link']=$site_domain.U('M/News/details_for_share',array('id'=>$newsid));
                $share['imgUrl']=$site_domain."/Public/assets/img/logo.png";

                $this->json_output($share, '成功', '失败');

                break;
                
            case 2:
                $DB = M("news");

                $res = $DB->find($newsid);

                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

                $share['title']=$res['title'];
        //        $share['desc']=$res['title'];
                $share['desc']="食品安全靠大家，一步一步更安全，我觉得这个信息很实用……";
                $share['link']=$site_domain.U('M/News/details_for_share',array('id'=>$newsid));
                $share['imgUrl']=$site_domain."/Public/assets/img/logo.png";

                $this->json_output($share, '成功', '失败');

                break;
                
            case 3:
                $DB = M("news");

                $res = $DB->find($newsid);

                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

        //        $share['desc']=$res['title'];
                $share['desc']="食品安全靠大家，一步一步更安全，我觉得这个信息很实用……";
                $share['imgUrl']=$site_domain."/Public/assets/img/logo.png";

                $this->json_output($share, '成功', '失败');

                break;

            default:
                $DB = M("news");

                $res = $DB->find($newsid);

                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

                $share['title']=$res['title'];
        //        $share['desc']=$res['title'];
                $share['desc']="食品安全靠大家，一步一步更安全，我觉得这个信息很实用……";
                $share['link']=$site_domain.U('M/News/details_for_share',array('id'=>$newsid));
                $share['imgUrl']=$site_domain."/Public/assets/img/logo.png";

                $this->json_output($share, '成功', '失败');
                break;
        }
        
    }


    /*
     * 获取指定食事周刊的分享
     */
    public function get_share_of_weekly() {
        $newsid = I('get.newsid');

        $type=I("get.type");
        
        if(empty($type))
        {
            $type=1;
        }
        switch ($type) {
            case 1:
                $DB = M("news");

                $res = $DB->find($newsid);

                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

                $share['title']=$res['title'];
        //        $share['desc']=$res['title'];
                $share['desc']="食品安全靠大家，一步一步更安全，我觉得这个信息很实用……";
                $share['link']=$site_domain.U('M/Information/details_for_share',array('id'=>$newsid));
                $share['imgUrl']=$site_domain."/Public/assets/img/logo.png";

                $this->json_output($share, '成功', '失败');

                break;
                
            case 2:
                $DB = M("news");

                $res = $DB->find($newsid);

                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

                $share['title']=$res['title'];
        //        $share['desc']=$res['title'];
                $share['desc']="食品安全靠大家，一步一步更安全，我觉得这个信息很实用……";
                $share['link']=$site_domain.U('M/Information/details_for_share',array('id'=>$newsid));
                $share['imgUrl']=$site_domain."/Public/assets/img/logo.png";

                $this->json_output($share, '成功', '失败');

                break;
                
            case 3:
                $DB = M("news");

                $res = $DB->find($newsid);

                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

        //        $share['desc']=$res['title'];
                $share['desc']="食品安全靠大家，一步一步更安全，我觉得这个信息很实用……";
                $share['imgUrl']=$site_domain."/Public/assets/img/logo.png";

                $this->json_output($share, '成功', '失败');

                break;

            default:
                $DB = M("news");

                $res = $DB->find($newsid);

                //判断环境
                if(!C('debug'))
                {
                    $site_domain = C('product_domain');
                }
                else
                {
                    $site_domain = C('development_domain');
                }

                $share['title']=$res['title'];
        //        $share['desc']=$res['title'];
                $share['desc']="食品安全靠大家，一步一步更安全，我觉得这个信息很实用……";
                $share['link']=$site_domain.U('M/Information/details_for_share',array('id'=>$newsid));
                $share['imgUrl']=$site_domain."/Public/assets/img/logo.png";

                $this->json_output($share, '成功', '失败');
                break;
        }
        
    }



    
    /*
     * 分享成功后的健康豆获取
     */
    public function get_share_finish() {
        $mid = I('get.mid');
        $newsid=I('get.newsid');
        
        $this->member_action_bonus("forward", $mid, 1);
        
        $this->json_output($mid);
    }
    
    /*
     * 获取标签信息
     */
    public function tags() {
//        仅仅获取4个
        $DB = M('product_tags');
        $map['active']=1;
        
        $res = $DB->where($map)->select();
        
        $this->json_output($res);
        
    }
    
    
    /*
     * 获取项目的state
     */
    private function _get_state_by_fdid($fdid) {
        $res = M('fundings')->find($fdid);
        return $res['state'];
        
    }
    
    /*
     * 获取文章详情的精彩评论
     * 原型参考
     * http://test.mdjkj.com/mdjkj/ZC2.1.2/#p=a_1_3_2_项目详情_众测中_
     */
    private function get_one_hot_comments($id,$ctype) {
//        有权重的方案
//        目前先按照评论数量来排序
        
        
        $map['for_id']=$id;
        $map['ctype']=$ctype;
        $map['pid']=0;
        
        $DB = M("comments");
        
        $res = $DB->where($map)->order("comment_count DESC")->limit(10)->select();
        if(!empty($res))
        {
            foreach ($res as $key => $value) {
                /*
                * 获取单个用户的头像信息
                */
                $res[$key]['avatar']=  $this->_get_one_avatar($value['mid']);
                $res[$key]['username']=  $this->_get_member_nickname($value['mid']);
                $res[$key]['child_comments'] = $this->_get_child_comment($value['id']);
            }
            
            return $res;
        }
        else{
            $result ="";
            return $result;
        }
    }
    
    /*
     * 获取评论的子评论
     * 
     * 最新的三条
     * 客服回答优先（hold）
     * 
     */
    public function _get_child_comment($comment_id) {
        
        $DB = M("comments");
        
        
        $map['ishidden'] = 0;
        $map['pid']=$comment_id;
        
        $res = $DB->where($map)->limit(3)->order("ctime desc")->select();
        foreach ($res as $key => $value) {
                /*
                * 获取单个用户的头像信息
                */
                $res[$key]['avatar']=  $this->_get_one_avatar($value['mid']);
                $res[$key]['username']=  $this->_get_member_nickname($value['mid']);
                
        }
            
        return $res;
        
    }
    
    
    /*
    * 判断当前用户是否关注了
    */
   private function chk_if_member_follow_founding($mid,$fdid,$ctype) {
       $map['ctype'] = $ctype;
       $map['for_id']=$fdid;
       $map['mid']=$mid;
       
       $DB = M("collections");
       
       $res = $DB->where($map)->find();
       
       if(!empty($res))
       {
           return 1;
           
       }
       else
       {
           return 0;
       }
   }
    
    /*
     * 获取单个用户的头像信息
     */
    private function _get_one_avatar($mid) {
        $member = $this->_get_member_info($mid);
        return $member['avatar'];
    }
    
    /*
     * 获取单个用户的昵称信息
     */
    private function _get_member_nickname($mid)
    {
//        $member = $this->_get_member_info($mid);
//        return $member['username'];
        $member = $this->_get_member_info($mid);
        $member_name=  $member['username'];
        if(mb_strlen($member_name)>8)
        {
            $member_name = mb_substr($member_name, 0,8,'utf-8');
        }
        return $member_name;
    }
    
    /*
     * 获取收藏资料
     * $return_type , 0 代表 是返回数字， 1代表返回数据
     */
    private function _get_member_collection($mid,$ctype,$return_type,$pagecount) {
        $map['ctype']=$ctype;
        $map['mid']=$mid;
        $pagecounts = $pagecount + 1;
        $res = M("collections")->where($map)->page($pagecounts,10)->order('ctime DESC')->group('for_id')->select();
        
        if($return_type==0)
        {
            return count($res);
        }
        else
        {
            if(!empty($res))
            {
                return $res;
            }
            else
            {
                return "";
            }
        } 
    }
   
    /**
     * 收藏文章接口
     */
    public function get_collection_news(){

        $mid = I('get.mid');
        $password = I('get.password');
        $pagecount = I('get.pagecount');
        // $ctype_t = I('get.ctype');
        $chk_user = $this->chk_user($mid,$password);
        if($chk_user){

            $ctype = array('in','2,6');
            $return_type = 1;
            $list = $this->_get_member_collection($mid,$ctype,$return_type,$pagecount);
            // print_r($list); die;
            foreach ($list as $k => $v) {
                # code...
                $temp = M('news')->where(array('id' => $v['for_id']))->find();

                $list[$k]['news_id'] = $temp['id'];
                $list[$k]['title'] = $temp['title'];
                $list[$k]['description'] = $temp['description'];
                $list[$k]['thumb'] = $temp['thumb'];
                $list[$k]['news_type'] = $temp['news_type'];
                if($v['ctype'] == 6){
                    $list[$k]['comment_count'] = M('comments')->where(array('for_id' => $v['for_id'],'ctype' => 6,'pid' => 0))->count();
                }else if($v['ctype'] == 2){

                    $list[$k]['comment_count'] = M('comments')->where(array('for_id' => $v['for_id'],'ctype' => 2,'pid' => 0))->count();
                }
                $list[$k]['url'] = 'http://'.I('server.HTTP_HOST').U('M/News/details_for_app',array('newsid'=>$v['for_id']));
                unset($list[$k]['ctype']);
            }

            if(!empty($list)){

                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['content'] = $list;
            }else{

                $res['err'] = 0;
                $res['msg'] = '没有数据!';
                $res['count'] = count($list);
                $res['content'] = '';
            }
            

        }else{

            $res['err'] = 1;
            $res['msg'] = '用户信息验证失败!';
            $res['content'] = '';
        }

        echo json_encode($res);
    }




    /**
     * 获取首页常见问题列表
     */
    function get_faq_list(){

        $news = M('news');
        $map['news_type'] = 2;

        $list = $news->where($map)->order("rank DESC,ctime DESC")->field('id,title,content')->select();

        foreach ($list as $key => $value) {
            # code...
            $list[$key]['content'] = htmlspecialchars_decode($value['content']);
        }
        if(!empty($list)){

            $res['err'] = 0;
            $res['msg'] = 'OK!';
            $res['content'] = $list;

        }else{

            $res['err'] = 1;
            $res['msg'] = '没有数据!';
            $res['content'] = '';
        }


        echo json_encode($res);
    }


    /**
     * 获取危害须知列表
     */
    public function get_harm_list(){

        $news = M('news');
        $map['news_type'] = 1;
        $map['active']=1;

        $list = $news->where($map)->field('id,title,news_type,content')->order('rank desc')->select();

        if(!empty($list)){

            if(!C('debug')){

                $site_domain = C('product_domain');
            }else{
                
                $site_domain = C('development_domain');
            }

            foreach ($list as $key => $value) {
                
                $list[$key]['url'] = $site_domain.U('M/News/harm_details_for_app',array('newsid'=>$value['id']));
            }

            $res['err'] = 0;
            $res['msg'] = 'OK!';
            $res['content'] = $list;
        }else{

            $res['err'] = 1;
            $res['msg'] = '没有数据!';
            $res['content'] = '';
        }

        echo json_encode($res);
    }

    /**
     * 获取危害须知列表
     */
    public function get_new_harm_list(){

        $news = M('news');
        $map['news_type'] = 1;
        $map['active']=1;
        
        for ($i=0; $i < 7 ; $i++) { 
            
            $map['pid'] = $i + 1;
            $list[$i]['pid'] = $i + 1;
            switch ($map['pid']) {
                case 1:
                    $list[$i]['item'] = '微生物';
                    break;
                case 2:
                    $list[$i]['item'] = '农药残留';
                    break;
                case 3:
                    $list[$i]['item'] = '兽药残留';
                    break;
                case 4:
                    $list[$i]['item'] = '食品添加剂';
                    break;
                case 5:
                    $list[$i]['item'] = '有毒有害物质';
                    break;
                case 6:
                    $list[$i]['item'] = '常规理化';
                    break;
                default:
                    $list[$i]['item'] = '其它检测指标';
                    break;
            }
            $list[$i]['item_detial'] = $news->where($map)->field('id,title,rank')->select();
        }
        
        if(!empty($list)){
            if(!C('debug')){

                $site_domain = C('product_domain');
            }else{
                
                $site_domain = C('development_domain');
            }
            
            $res['err'] = 0;
            $res['msg'] = 'OK!';
            $res['content'] = $list;
        }else{

            $res['err'] = 1;
            $res['msg'] = 'NULL';
            $res['content'] = '';
        }

        echo json_encode($res);
    }


    /**
     * 获取危害简介文章内容
     */
    public function get_harm_detial(){

        $id = I('get.id');
        if(!empty($id)){

            $map['id'] = $id;
            $map['news_type'] = 1;
            $map['active'] = 1;
            $DB = M('news');

            $list = $DB->where($map)->field('title,content,ctime')->find();
            if (!empty($list)) {
                
                $res['err'] = 0;
                $res['msg'] = 'OK!';
                $res['content'] = $list;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'NULL';
                $res['content'] = '';
            }
        }else{

            $res['err'] = 1;
            $res['msg'] = '参数不能为空';
            $res['content'] = '';
        }

        echo json_encode($res);
    }


    /**
     * 获取食事周刊列表
     */
    public function get_weekly_list(){

        $pagecount = I('get.pagecount') + 1;
        $map['news_type'] = 3;
        $news = M('news');

        $list = $news->where($map)->page($pagecount,10)->field('id,thumb,title,news_type ,ctime')->order("rank desc")->select();
        /**
         * 获取食事周刊的评论数
         */
        if(!C('debug')){

            $site_domain = C('product_domain');
        }else{
            
            $site_domain = C('development_domain');
        }

        $ctype = 6;
        foreach ($list as $key => $value) {
            # code...
            $list[$key]['comment_count'] = $this->get_one_comments_count($value['id'],$ctype);
            $list[$key]['url'] = $site_domain.U('M/News/weekly_details_for_app',array('newsid'=>$value['id']));
        }

        if(!empty($list)){

            $res['err'] = 0;
            $res['msg'] = 'OK!';
            $res['content'] = $list;
        }else{

            $res['err'] = 0;
            $res['msg'] = '没有数据!';
            $res['content'] = array();
        }


        echo json_encode($res);
    }


    /**
     * 获取指定食事周刊的热门评论
     */
    public function  weekly_get_one(){

        $id = I('get.id');

        $list =  $this->get_one_hot_comments($id,6);

        if(!empty($list)){

            $res['err'] = 0;
            $res['msg'] = 'OK!';
            $res['content'] = $list;
        }else{

            $res['err'] = 1;
            $res['msg'] = '没有数据!';
            $res['content'] = '';
        }

        echo json_encode($res);
    }



    /*
     * 获取评论数
     */
    private function get_one_comments_count($id,$ctype) {
        $map['for_id']=$id;
        $map['ctype']=$ctype;
        $map['pid']=0;
        $DB = M("comments");
        
        $res = $DB->where($map)->count();
        if(!empty($res))
        {
            return $res;
        }
        else{
            return 0;
        }  
    }



    /**
     * 新闻测试接口
     */
    public function get_news_code_html(){

        $DB = M('news');

        $list = $DB->where(array('news_type' => 0))->field('title,content')->limit(1)->select();
        // html_entity_decode($list[0]['content']);
        
        if(!empty($list)){

            $res['err'] = 0;
            $res['msg'] = 'OK';
            $res['content']['title'] = $list[0]['title'];
            $res['content']['content'] = $list[0]['content'];
        }

        // print_r($list); die;
        echo json_encode($res);
    }


    /**
     * 根据news_type  id获取资讯的详情
     */
    public function get_htmlencode_by_type(){

        $id = I('get.id');
        $mid = I('get.mid');
        if(empty($id)){
            $res['err'] = 1;
            $res['msg'] = '缺少id参数';
            $res['content'] = '';
        }else{

            $DB = M('news');
            $map['id'] = $id;
            $map['active'] = 1;

            $list = $DB->where($map)->field('id,title,description,content,thumb,ctime,stime,author,news_type')->find();
//            print_r($DB->getLastSql());
            
            switch ($list['news_type']) {
                case 0:
                    $ctype = 2;
                    break;
                case 1:
                    $ctype = 4;
                    break;
                case 2:
                    $ctype = 5;
                    break;
                default:
                    $ctype = 6;
                    break;
            }
            //获取对应的热门评论
            $list['hot_comments'] = $this->get_one_hot_comments($list['id'],$ctype);
            //获取评论数
            $list['comment_count'] = $this->get_one_comments_count($list['id'],$ctype);
            //判断是否关注
            $list['is_collection']  = $this->chk_if_member_follow_founding($mid,$list['id'],$ctype);

            if(!empty($list)){
                $res['err'] = 0;
                $res['msg'] = 'OK';
                $res['content'] = $list;
            }else{

                $res['err'] = 1;
                $res['msg'] = 'NULL';
                $res['content'] = '';
            }
        }
        echo json_encode($res);
    }

}
